using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;

namespace aKsUserator
{
    public class userator
    {
        public bool Login()
        {

            DBOperator dbOper = new DBOperator();
            usrLogin loginForm = new usrLogin();

            loginForm.ShowDialog();

            if (loginForm.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                string login = loginForm.txtLogin.Text;
                string password = loginForm.txtPassword.Text;
                string[] roles;

                if (dbOper.verifyUser(login, password))
                {
                    GenericIdentity identity = new GenericIdentity(login);
                    roles = dbOper.getUserRoles(login);
                    GenericPrincipal prince = new GenericPrincipal(identity, roles);

                    Thread.CurrentPrincipal = prince;

                    return true;
                }
                else
                {
                    MessageBox.Show("not verified successfully");
                    return false;
                }
            }
            //usrLogin loginForm = new usrLogin();

            //loginForm.ShowDialog();

            //if (loginForm.DialogResult == System.Windows.Forms.DialogResult.OK)
            //{
            //    string login = loginForm.txtLogin.Text;
            //    string password = loginForm.txtPassword.Text;
            //    string[] roles;

            //    if (login == "aks" && password == "aks")
            //    {
            //        GenericIdentity identity = new GenericIdentity(login);
            //        roles = new string[]{"manager"};
            //        GenericPrincipal prince = new GenericPrincipal(identity, roles);

            //        Thread.CurrentPrincipal = prince;

            //        return true;
            //    }
            //    else if(login == "kowalski" && password == "kowalski")
            //    {
            //        GenericIdentity identity = new GenericIdentity(login);
            //        roles = new string[]{"zwyklak"};
            //        GenericPrincipal prince = new GenericPrincipal(identity, roles);

            //        Thread.CurrentPrincipal = prince;

            //        return true;
            //    }
            //    else
            //    {
            //        MessageBox.Show("not verified successfully");
            //        return false;
            //    }
            //}

            return false;
        }

        public void Logout()
        {
            Thread.CurrentPrincipal = null;
        }
    }
}
